package com.ffcs.web.utils;

import java.io.StringWriter;


//import net.bean.TypeBean;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
import net.sf.json.JSONObject;

public class JsonUtil {
	
	private static ObjectMapper objectMapper=new ObjectMapper();
 
	/**
	 * 将对象转换成json格式的字符串
	 */
	public static String objectToJson(Object obj){  	
        JsonGenerator jsonGenerator=null;
        String str="";
        try {
        	StringWriter writer = new StringWriter();
        	jsonGenerator=objectMapper.getJsonFactory().createJsonGenerator(writer);
		    jsonGenerator.writeObject(obj);
		    str = writer.toString();
        } catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (jsonGenerator != null) {
				try {
					jsonGenerator.close();
					jsonGenerator = null;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
        return str;
    }
	
	/**
	 * @description:json转换为bean
	 * @author: dairf Apr 8, 2013
	 * @param str
	 * @param cla
	 * @return
	 */
	public static Object jsonToBean(String str,Class cla){
		JSONObject jo = new JSONObject();
		jo=JSONObject.fromObject(str);
		return JSONObject.toBean(jo, cla);
	}
}
